package com.game.spacetrader;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.util.ArrayList;

import com.game.spacetrader.map.MapView;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.Environment;
import android.util.Log;
import android.view.View;
import android.view.Window;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;

/**
 * Class to handle the loading of saved game files
 */
public class LoadGame extends Activity {
	/**
	 * Creates the page
	 */
	public void onCreate(Bundle savedInstanceState){
		super.onCreate(savedInstanceState);
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		setContentView(R.layout.loadgame);
		
		ArrayList<String> fileNames = new ArrayList<String>();
		File sdcard = Environment.getExternalStorageDirectory();
		
		
		String files;
		File folder = new File (sdcard.getAbsolutePath() + "/Android/com.game.spacetrader");
		File[] listOfFiles = folder.listFiles(); 
		//Log.d("test2222", "" + listOfFiles.length);
		for (int i = 0; i < listOfFiles.length; i++) {
			if (listOfFiles[i].isFile()) {
				files = listOfFiles[i].getName();
		        if (files.endsWith(".stdata") || files.endsWith(".STDATA")) {
		        	fileNames.add(files);
		        }
			}
		}
		final ArrayAdapter<String> arrayAdapter =      
		        new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1, fileNames);
		ListView loadList = (ListView) findViewById(R.id.loadList);
		loadList.setAdapter(arrayAdapter); 
		loadList.setOnItemClickListener(new AdapterView.OnItemClickListener() {
			/**
			 * Starts game based on file selected
			 */
			public void onItemClick(AdapterView<?> arg0, final View v, int index,
					long id) {
				
				try { 
					Game.getInstance().setGame(LoadGame.loadGame(((TextView) v).getText().toString()));
				} catch(Exception e) {
					Log.d("", e.toString() + "testtesttetst");
				}
				Intent i = new Intent(LoadGame.this, MapView.class);
				startActivity(i);
			}
		});
	}
	
	/**
	 * Returns a Game object based on the filename the user picks
	 * 
	 * @param fileName
	 * @return
	 */
	public static Game loadGame(String fileName) {
		ObjectInputStream inputStream = null;
		try {
			File sdcard = Environment.getExternalStorageDirectory();
			File dir = new File (sdcard.getAbsolutePath() + "/Android/com.game.spacetrader");
			File file = new File(dir, fileName);
            inputStream = new ObjectInputStream(new FileInputStream(file));
            
            try {
				return (Game) inputStream.readObject();
			} catch (ClassNotFoundException e1) {
				Log.d(e1.toString(), " test8");
				return null;
			}
            
        } catch (FileNotFoundException ex) {
        	Log.d(ex.toString(), "test5");
        	return null;
        } catch (IOException ex) {
        	Log.d(ex.toString(), "test6");
        	return null;
        } finally {        	
            try {
                if (inputStream != null) {
                    inputStream.close();
                }
            } catch (IOException ex) {
                Log.d(ex.toString(), "test7");
            }
        }
	}
}
